<?php

define('FULL', 1);
define('LAST', 2);
define('FULL_ARRAY', 3);

class Bootstrap {

    function __construct() {

        $url = isset($_GET['url']) ? $_GET['url'] : null;
        $url = rtrim($url, '/');
        $url = filter_var($url, FILTER_SANITIZE_URL);
        $url = explode('/', $url);

        //print_r($url);

        if (empty($url[0])) {
            require 'controllers/index.php';
            $controller = new Index();
            $controller->index();
            return false;
        }

        $file = 'controllers/' . $url[0] . '.php';
        if (file_exists($file)) {
            require $file;
        } else {
            $this->error();
        }
        $class_name = $url[0];
        if (class_exists($class_name)) {
            $controller = new $class_name;
            $controller->loadModel($url[0]);
            // calling methods
            if (isset($url[2])) {
                if ($url[1] == 'page') {
                    $controller->index();
                } else {
                    if ($url[2] == 'page') {
                        if (method_exists($controller, $url[1])) {
                            $controller->{$url[1]}();
                        } else {
                            $this->error();
                        }
                    } else {
                        if (method_exists($controller, $url[1])) {
                            $controller->{$url[1]}($url[2]);
                        } else {
                            $this->error();
                        }
                    }
                }
            } else {
                if (isset($url[1])) {
                    if ($url[1] == 'page') {
                        $controller->index();
                    } else {
                        if (method_exists($controller, $url[1])) {
                            $controller->{$url[1]}();
                        } else {
                            $this->error();
                        }
                    }
                } else {
                    $controller->index();
                }
            }
        }
    }

    function error() {
        require 'controllers/error.php';
        $controller = new Error();
        $controller->index();
        return false;
    }

    public static function parametro_url($posicion, $parameter = NULL) {
        if (isset($_GET) AND array_key_exists('url', $_GET)) {
            $url = $_GET['url'];
            $url = rtrim($url, '/');
            $url = explode('/', $url);
            if ($url[0] <> SYSTEM) {
                foreach ($url as $key => $value) {
                    $url2[$key + 1] = $value;
                }
                unset($url);
                $url = $url2;
            }
            if ($parameter == NULL) {
                if (array_key_exists($posicion, $url)) {
                    return $url[$posicion];
                } else {
                    return FALSE;
                }
            } elseif ($parameter == 1) {
                unset($url[0]);
                return implode('/', $url);
            } elseif ($parameter == 2) {
                unset($url[0]);
                return $url[count($url)];
            } elseif ($parameter == 3) {
                return $url;
            }
        }
    }

}